<template>
  <div class="field-list">
    <el-button @click="addField" size="mini">添加</el-button>
    <el-table
      :data="value"
      border
      size="mini"
      style="width: 100%">
      <el-table-column
        prop="name"
        label="名称"
        minWidth="100">
      </el-table-column>
      <el-table-column
        prop="expression"
        label="表达式">
      </el-table-column>
      <el-table-column
        prop="stringValue"
        label="字符串">
      </el-table-column>
      <el-table-column
        prop="string"
        label="长文本">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="{ row }">
          <el-button type="text" size="mini" @click="editField(row)">编辑</el-button>
          <el-button type="text" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="visible" append-to-body>
      <el-form ref="from" :model="fieldElm" label-width="120px" size="mini">
        <el-form-item label="字段名称" prop="name">
          <el-input v-model="fieldElm.name"></el-input>
        </el-form-item>
        <el-form-item label="字段表达式" prop="expression">
          <el-input v-model="fieldElm.expression"></el-input>
        </el-form-item>
        <el-form-item label="字段字符串" prop="stringValue">
          <el-input v-model="fieldElm.stringValue"></el-input>
        </el-form-item>
        <el-form-item label="字段长文本" prop="string">
          <el-input
            type="textarea"
            :rows="2"
            v-model="fieldElm.string"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="saveField">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
    import bpmnHelper from '../bpmnHelper'

    export default {
        mixins: [bpmnHelper],
        components: {},
        props: {
            value: {
                type: Array,
                default() {
                    return []
                }
            }
        },
        data() {
            return {
                fieldList: this.value,
                fieldElm: {
                    stringValue: '',
                    expression: '',
                    string: '',
                    name: '',
                },
                fieldIndex: 0,
                visible: false,
            }
        },
        computed: {},
        watch: {
            value(n) {
                this.fieldList = n
            },
            fieldList(n) {
                this.$emit('input', n)
            },
            visible(n) {
                if (!n) {
                    this.$refs.from.resetFields()
                }
            }
        },
        methods: {
            addField() {
                this.fieldIndex = 0
                this.visible = true
            },
            editField(element) {
                this.fieldIndex = this.fieldList.indexOf(element)
                this.visible = true
                //防止表单值无法重置
                this.$nextTick(() => {
                    for (const key in this.fieldElm) {
                        if (element.hasOwnProperty(key)) {
                            this.fieldElm[key] = element[key]
                        }
                    }
                })
            },
            saveField() {
                const taskField = this.createField(this.fieldElm)
                if (this.fieldIndex) {
                    this.fieldList.splice(this.fieldIndex, 1, taskField)
                } else {
                    this.fieldList.push(taskField)
                }
                this.visible = false
            }
        }
    }
</script>
